/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao.xml;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

/**
 *
 * @author
 * PedroLZ
 */
public class GenericsXmlDAO 
{
	//-----------------------------------------------------------
	protected XMLEncoder getXmlEncoder(String filePath) throws FileNotFoundException
	{
		try 
		{
			return new XMLEncoder(new FileOutputStream(filePath));
		} catch (FileNotFoundException e){
			throw new FileNotFoundException("Erro ao tentar gravar os dados.");
		}
	}
	
	//-----------------------------------------------------------
	protected XMLDecoder getXmlDecoder(String filePath) throws FileNotFoundException
	{
		try 
		{
			return new XMLDecoder(new FileInputStream(filePath));
		} catch (FileNotFoundException e){
			throw new FileNotFoundException("Erro ao tentar ler os dados.");
		}
	}
	
	//-----------------------------------------------------------
	protected void fecharConexao(XMLEncoder xmlEncoder){
		try{
			if (xmlEncoder != null)
				xmlEncoder.close();
		}catch(IllegalStateException e){
			throw new IllegalStateException("Erro ao finalizar a gravação dos dados.");
		}
	}

	//-----------------------------------------------------------
	protected void fecharConexao(XMLDecoder xmlDecoder){
		try{
			if (xmlDecoder != null)
				xmlDecoder.close();
		}catch(IllegalStateException e){
			throw new IllegalStateException("Erro ao finalizar a leitura dos dados.");
		}
	}	
}
